﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityValue
{
    public class MyProperty : Property { }
    public class MyEntity : Entity<MyProperty>
    {

    }

    public class YourProperty : Property { }
    public class YourEntity : Entity<YourProperty>
    {

    }
    public class Entity<TProperty> where TProperty : Property, new()
    {

        public Entity()
        {
            Properties = new List<TProperty>();
        }
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public ICollection<TProperty> Properties { get; set; }
        public TProperty this[string propertyName]
        {
            get { 
                if ( Properties.Any(m => m.Name == propertyName))
                    return Properties.Where(m => m.Name == propertyName).FirstOrDefault();
                else
                {
                    TProperty newProperty = new TProperty();
                    newProperty.Name = propertyName;
                    Properties.Add(newProperty);
                    return newProperty;
                }
                }
            
            set
            {
                Properties.Remove(Properties.Where(m => m.Name == propertyName).FirstOrDefault());
                Properties.Add(value);
            }
        }

    }
    public class Property
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int PropertyId { get; set; }
        public string Name { get; set; }
        public RefValue Value { get; set; }
    }

    public abstract class RefValue 
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ValueId { get; set; }
    }
    public interface IValue<T> 
    {
        T Value { get; set; }
    }


    public class StringValue : RefValue
    {
        public string Value { get; set; }
    }

    public class DateValue : RefValue
    {
        public DateTime Value { get; set; }
    }
}
